# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=coin3d
pkgbase=mingw-w64-${_realname}-hg
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-hg"
pkgver=r11819+.c0999df53040+
pkgrel=1
pkgdesc="High-level, retained-mode toolkit for effective 3D graphics development (mingw-w64)"
arch=('any')
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-simage"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-superglu"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("mercurial" "${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-gcc")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
url='https://bitbucket.org/Coin3D/coin'
source=("${_realname}"::"hg+https://bitbucket.org/Coin3D/coin"
        "hg+https://bitbucket.org/Coin3D/generalmsvcgeneration"
        "hg+https://bitbucket.org/Coin3D/boost-header-libs-full"
        001-cast-errors.patch
        002-fix-building-shared-lib.patch
        003-install-pkg-config.patch
        004-fix-library-name-mingw.patch)
options=(!strip staticlibs)
sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            '3443689e588ee951c67a411c9541c4d19cca3d8d73a5d91ef7f45cfb6f8e114a'
            '660a698dfb9be788687cfac872aef147b54602127627bf5bb77ddb4df5772bd2'
            'ae2b241c344c04417c2351dc5de4be3231be06ea2544d0ba8f5301b9e7d1c1c7'
            'd94ff84e223c6b44b1152923d50d2b77709ef9a01a34d2386327872b78c3e8b4')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "r%s.%s" "$(hg identify -n)" "$(hg identify -i)"
}

prepare() {
  cd ${srcdir}/${_realname}
  patch -p1 -i ${srcdir}/001-cast-errors.patch
  patch -p1 -i ${srcdir}/002-fix-building-shared-lib.patch
  patch -p1 -i ${srcdir}/003-install-pkg-config.patch
  patch -p1 -i ${srcdir}/004-fix-library-name-mingw.patch
}

build() {
  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=( -DCMAKE_BUILD_TYPE=Debug )
  else
    extra_config+=( -DCMAKE_BUILD_TYPE=Release )
  fi

  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCOIN_BUILD_INTERNAL_DOCUMENTATION=ON \
    -DCOIN_BUILD_SHARED_LIBS=ON \
    -DCOIN_BUILD_SINGLE_LIB=ON \
    -DCOIN_BUILD_DOCUMENTATION=OFF \
    -DCOIN_VERBOSE=ON \
    ${extra_config} \
    ../${_realname}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
